package com.itheima.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.api.client.dto.TagDto;
import com.itheima.blog.admin.dao.TagMapper;
import com.itheima.blog.admin.dto.TagReqDto;
import com.itheima.blog.admin.entity.Tag;
import com.itheima.blog.admin.service.TagService;
import com.itheima.blog.util.constants.Constants;
import com.itheima.blog.util.constants.ErrorInfo;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.exception.BlogException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 标签表 服务实现类
 *
 * @author xujiashuai
 * @date 2018-09-08
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public PageResDto<Tag> getPageList(TagReqDto tagReqDto) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(tagReqDto.getContent()), Tag::getContent, tagReqDto.getContent());
        queryWrapper.eq(tagReqDto.getStatus()!=null, Tag::getStatus, tagReqDto.getStatus());
        queryWrapper.orderByAsc(Tag::getSort);
        Page<Tag> page = new Page<>();
        page.setCurrent(tagReqDto.getPage());
        page.setSize(tagReqDto.getSize());
        IPage<Tag> pageList = this.page(page, queryWrapper);
        return new PageResDto(tagReqDto, pageList.getTotal(), pageList.getRecords());
    }

    @Override
    public List<Tag> getList() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( Tag::getStatus, Constants.STATUS_ENABLE);
        queryWrapper.orderByAsc(Tag::getSort);
        List<Tag> tagList = this.list(queryWrapper);
        return tagList;
    }

    @Override
    public Integer addTag(TagReqDto tagReqDto) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getContent, tagReqDto.getContent());
        Tag tagDB = this.getOne(queryWrapper);
        if (tagDB != null) {
            throw new BlogException(ErrorInfo.Msg.DATA_EXIST);
        }
        Tag tag = new Tag();
        tag.setContent(tagReqDto.getContent());
        tag.setStatus(Constants.STATUS_ENABLE);
        tag.setSort(tagReqDto.getSort());
        tag.setCreateTime(new Date());
        tag.setUpdateTime(new Date());
        this.save(tag);
        return tag.getId();
    }

    @Override
    public boolean editTag(TagReqDto tagReqDto) {
        Tag tagById = this.getById(tagReqDto.getId());
        if(tagById == null){
            throw new BlogException(ErrorInfo.Msg.DATA_NOT_EXIST);
        }
        // 判断是否重复
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getContent, tagReqDto.getContent());
        Tag tagByContent = this.getOne(queryWrapper);
        if (tagByContent != null && !Objects.equals(tagByContent.getId(),tagById)) {
            throw new BlogException(ErrorInfo.Msg.DATA_EXIST);
        }
        Tag tag = new Tag();
        tag.setContent(tagReqDto.getContent());
        tag.setStatus(tagReqDto.getStatus());
        tag.setSort(tagReqDto.getSort());
        tag.setUpdateTime(new Date());
        tag.setId(tagReqDto.getId());
        this.updateById(tag);
        return true;
    }

    @Override
    public boolean deleteTag(TagReqDto tagReqDto) {
        //TODO 如果有博客引用了标签，则不能删除
        return true;
    }

    @Override
    public boolean deleteBatchTag(List<TagReqDto> tagReqDtoList) {
        //TODO 如果有博客引用了标签，则不能删除
        return true;
    }

    @Override
    public TagDto getTagByName(String tagName) {
        Tag tag = lambdaQuery().eq(Tag::getContent, tagName).one();
        TagDto tagDto = new TagDto();
        tagDto.setContent(tag.getContent());
        tagDto.setId(tag.getId());
        return tagDto;
    }

}
